import React, { Component } from 'react'
import store from './store'

class App extends Component {
  componentDidMount() {
    // 订阅
    this.unsubscribe = store.subscribe(() => {
      this.forceUpdate()
    })
  }

  componentWillUnmount() {
    // 取消订阅
    this.unsubscribe()
  }

  render() {
    // 获取数据
    const { count } = store.getState()
    return (
      <div>
        <h3>count:{count}</h3>
        <button
          onClick={() => {
            // dispatch中的type属性它是必须要存在
            store.dispatch({ type: 'add', payload: 10 })
          }}
        >
          ++count++
        </button>
      </div>
    )
  }
}

export default App
